<?php
	require("libraries/student.php");
	class Services extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_service");
			$this->load->library("string");
			$this->load->helper(array("form","date"));
			$this->load->library("form_validation");	
		}
		public function index(){
			$config['base_url'] = base_url().'admin/services/index/';
			$config['total_rows'] = $this->model_service->count_all();
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$config['first_link'] = "First";
			$config['last_link'] = "Last";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['title']= "Manage services";
			$data['act'] = 11;
			$data['listtrain'] = $this->model_service->listtrain();
			$data['data']['info']= $this->model_service->list_all($config['per_page'],$start);
			$data['template'] = "services/list_service";
			$this->load->view("layout",$data);
		}
		public function add(){
			$data['error'] = "";
			$data['data'] = "";
			$data['act'] = 11;
			$data['title'] = "Manage services";
			$data['template'] = "services/add_service";
			$data['listtrain'] = $this->model_service->listtrain();
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("service_title","Service title","min_length[5]|require");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					  $title = trim($this->input->post('service_title'));
					  $db = array(
							"service_title" => $title,
							"service_adults" => $this->input->post("service_adults"),
							"service_child" => $this->input->post("service_child"),
							"service_all" => $this->input->post("service_all"),
							"service_type"  => $this->input->post("service_type"),
							"service_status" => 1,
							"train_id" => $this->input->post("train_id")
					  );
					 $this->model_service->add($db);
					 redirect(base_url()."admin/services/index");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['error'] = "";
			$data['data'] = "";
			$data['act'] = 11;
			$data['title'] = "Manage services";
			$data['template'] = "services/edit_service";
			$data['listtrain'] = $this->model_service->listtrain();
			$data['getdata'] = $this->model_service->getdata($id);
			$data['train_id'] = $data['getdata']['train_id'];
			if($data['getdata'] == NULL){
				die("Error!");
			}
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("service_title","Service title","min_length[5]|require");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					  $title = trim($this->input->post('service_title'));
					  $db = array(
							"service_title" => $title,
							"service_adults" => $this->input->post("service_adults"),
							"service_child" => $this->input->post("service_child"),
							"service_all" => $this->input->post("service_all"),
							"service_type"  => $this->input->post("service_type"),
							"service_status" => $this->input->post("status"),
							"train_id" => $this->input->post("train_id")
					  );
					 $this->model_service->update($db,$id);
					 redirect(base_url()."admin/services/index");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function select(){
			$val = $this->input->post("val");
			$data = $this->model_service->listservice($val);
			if($data != NULL){
				echo '<table cellpadding="0" cellspacing="0" width="905">
						<tbody><tr>
							<th width="25">Stt.</th>
							<th width="287">Service title</th>
							<th width="108">Price </th>
                            <th width="137">Price Promotion</th>
							<th width="188">Ship name</th>
                            <th width="84">Status</th>
							<th width="74">Actions</th>
						</tr>';
				$stt = 0;
				foreach($data as $items){
					$stt++;
					echo "<tr>";	
						echo "<td>$stt</td>";
							echo "<td>".$items['service_title']."</td>";
							echo "<td>".$items['service_adults']."</td>";
							echo "<td>".$items['service_child']."</td>";
							echo "<td>".$items['train_name']."</td>";
							if($items['service_status'] == 1){
								echo "<td>Active</td>";
							}else{
								echo "<td>Not active</td>";
							}
							echo "<td>";
								echo "<div class='actions_menu'><ul>";
									echo "<li><a href='".base_url()."admin/services/update/$items[service_id]/' class='edit'>Edit</a></li>";
									echo "<li><a href='".base_url()."admin/services/del/$items[service_id]/' class='delete' onclick='return check()'>Del</a></li>";
								echo "</ul></div>";
							echo "</td>";
					echo "</tr>";
				}
				echo "</table>";
			}else{
				echo "No result";
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$this->model_service->del($id);
			redirect(base_url()."admin/services");
		}
	}